<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\facade\Db;
use think\Exception;

class Area extends Backend
{
    /**
     * Hospital模型对象
     * @var \app\common\model\Area
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Area;
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $open = $this->request->post('open','');

            Db::startTrans();

            try {

                \app\common\model\Area::update(['open'=>0],[['id','>',0]]);

                if($open){
                    $open_arr=explode(',',$open);
                    \app\common\model\Area::update(['open'=>1],['id'=>$open_arr]);
                }
                Db::commit();
            }catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }

        $selected=$this->model->where(['open'=>1])->column('id')?:[];
        $treeList = $this->model->getTreeList($selected);
        $this->assign('open', implode(',',$selected));
        $this->assign('treeList', $treeList);

        return $this->view->fetch();
    }
}